﻿using DDU.Infrastructure;
using System.Windows.Media;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using System;
using System.ComponentModel;
using DDU.Enums;

namespace DDU.DataModel
{
    public class OptionInfo : NotificationObject
    {
        public OptionInfo()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                CheckSettings();
            }
        }

        #region Fields
        private string backgroundImage = "/DDU.Curriculum;component/Assets/back.png";
        private string language = "zh-CN";
        private ImageLocationValue location = ImageLocationValue.Applocation;
        private Stretch backgroundImageStretch = Stretch.UniformToFill;
        private bool showAllActivities = false;
        private bool showAllVacations = false;
        private long cellNetworkDataLength = 0;
        private long wifiNetworkDataLength = 0;
        private bool advancedLocked = true;
        #endregion

        #region Properties
        public string BackgroundImage
        {
            get { return backgroundImage; }
            set
            {
                backgroundImage = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.ImageName] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => BackgroundImage);
            }
        }

        public string Language
        {
            get { return language; }
            set
            {
                language = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.Language] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => Language);
            }
        }

        public ImageLocationValue Location
        {
            get { return location; }
            set
            {
                location = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.Location] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => Location);
            }
        }

        public bool ShowAllActivities
        {
            get
            {
                return showAllActivities;
            }
            set
            {
                showAllActivities = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.ShowAllActivities] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => ShowAllActivities);
            }
        }

        public Stretch BackgroundImageStretch
        {
            get { return backgroundImageStretch; }
            set
            {
                backgroundImageStretch = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.ImageStretch] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => BackgroundImageStretch);
            }
        }

        public bool ShowAllVacations
        {
            get { return showAllVacations; }
            set
            {
                showAllVacations = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.ShowAllVacations] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => ShowAllVacations);
            }
        }

        public IEnumerable<Stretch> StretchCollection
        {
            get
            {
                yield return Stretch.Fill;
                yield return Stretch.None;
                yield return Stretch.Uniform;
                yield return Stretch.UniformToFill;
            }
        }

        public long CellNetworkDataLength
        {
            get { return cellNetworkDataLength; }
            set
            {
                cellNetworkDataLength = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.CellNetworkDataLength] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => CellNetworkDataLength);
            }
        }

        public long WiFiNetworkDataLength
        {
            get { return wifiNetworkDataLength; }
            set
            {
                wifiNetworkDataLength = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.WiFiNetworkDataLength] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => WiFiNetworkDataLength);
            }
        }

        public bool AdvancedLocked
        {
            get { return advancedLocked; }
            set
            {
                advancedLocked = value;
                RaisePropertyChanged(() => AdvancedLocked);
            }
        }
        #endregion

        #region Methods
        private void CheckSettings()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            #region Background
            if (!settings.Contains(OptionKeys.ImageName))
            {
                settings.Add(OptionKeys.ImageName, backgroundImage);
                settings.Save();
            }
            else
            {
                BackgroundImage = settings[OptionKeys.ImageName].ToString();
            }
            #endregion

            #region Language
            if (!settings.Contains(OptionKeys.Language))
            {
                settings.Add(OptionKeys.Language, language);
                settings.Save();
            }
            else
            {
                Language = settings[OptionKeys.Language].ToString();
            }
            #endregion

            #region Image Location
            if (!settings.Contains(OptionKeys.Location))
            {
                settings.Add(OptionKeys.Location, location);
                settings.Save();
            }
            else
            {
                Location = (ImageLocationValue)settings[OptionKeys.Location];
            }
            #endregion

            #region ShowAllActivities
            if (!settings.Contains(OptionKeys.ShowAllActivities))
            {
                settings.Add(OptionKeys.ShowAllActivities, showAllActivities);
                settings.Save();
            }
            else
            {
                string _value = settings[OptionKeys.ShowAllActivities].ToString();
                ShowAllActivities = bool.Parse(_value);
            }
            #endregion

            #region BackgroundImageStretch
            if (!settings.Contains(OptionKeys.ImageStretch))
            {
                settings.Add(OptionKeys.ImageStretch, backgroundImageStretch);
                settings.Save();
            }
            else
            {
                BackgroundImageStretch = (Stretch)settings[OptionKeys.ImageStretch];
            }
            #endregion

            #region ShowAllVacations
            if (!settings.Contains(OptionKeys.ShowAllVacations))
            {
                settings.Add(OptionKeys.ShowAllVacations, showAllVacations);
                settings.Save();
            }
            else
            {
                ShowAllVacations = bool.Parse(settings[OptionKeys.ShowAllVacations].ToString());
            }
            #endregion

            #region CellNetworkDataLength
            if (!settings.Contains(OptionKeys.CellNetworkDataLength))
            {
                settings.Add(OptionKeys.CellNetworkDataLength, cellNetworkDataLength);
                settings.Save();
            }
            else
            {
                CellNetworkDataLength = Convert.ToInt64(settings[OptionKeys.CellNetworkDataLength].ToString());
            }
            #endregion

            #region WiFiNetworkDataLength
            if (!settings.Contains(OptionKeys.WiFiNetworkDataLength))
            {
                settings.Add(OptionKeys.WiFiNetworkDataLength, wifiNetworkDataLength);
                settings.Save();
            }
            else
            {
                WiFiNetworkDataLength = Convert.ToInt64(settings[OptionKeys.WiFiNetworkDataLength].ToString());
            }
            #endregion

            #region AdvancedLocked
            if (!settings.Contains(OptionKeys.AdvancedLocked))
            {
                settings.Add(OptionKeys.AdvancedLocked, advancedLocked);
                settings.Save();
            }
            else
            {
                AdvancedLocked = bool.Parse(settings[OptionKeys.AdvancedLocked].ToString());
            }
            #endregion
        }
        #endregion

        #region Event
        public event EventHandler SettingsChanged = delegate { };

        protected override void RaisePropertyChanged(string propertyName)
        {
            base.RaisePropertyChanged(propertyName);
            SettingsChanged.Invoke(this, new EventArgs());
        }

        public void Reset()
        {
            IsolatedStorageSettings.ApplicationSettings.Clear();
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.ShowAllActivities, false);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.ImageName, "/DDU.Curriculum;component/Assets/back.png");
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.ImageStretch, Stretch.UniformToFill);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.Language, "zh-CN");
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.Location, ImageLocationValue.Applocation);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.ShowAllVacations, false);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.CellNetworkDataLength, 0);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.WiFiNetworkDataLength, 0);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.AdvancedLocked, false);
            IsolatedStorageSettings.ApplicationSettings.Save();

            CheckSettings();
        }

        public void Save()
        {
            IsolatedStorageSettings.ApplicationSettings.Save();
        }
        #endregion

    }
}
